package com.luo.divview3;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 自定义进度条 CustomProgressBar
 * <p>
 * Created by Luo on 2017/4/27 0027.
 */
public class CustomProgressBar extends View {
    // 自定义属性
    private int firstColor;
    private int secondColor;
    private int circleWidth;
    private int speed;

    /*画笔*/
    private Paint mPaint;
    /*当前进度*/
    private int mProgress;
    /*是否应该开始下一个*/
    private boolean isNext;
    /*圆的对象*/
    private RectF oval;

    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0);
        int n = a.getIndexCount();
        // 解析自定义属性，并赋值
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomProgressBar_firstColor:
                    firstColor = a.getColor(attr, Color.GREEN);
                    break;
                case R.styleable.CustomProgressBar_secondColor:
                    secondColor = a.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomProgressBar_circleWidth:
                    circleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomProgressBar_speed:
                    speed = a.getInt(attr, 20);
                    break;
            }
        }
        // 回收资源
        a.recycle();
        // 这边初始化画笔
        mPaint = new Paint();
        oval = new RectF();
        // 这里让他一直动
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    mProgress++;
                    if (mProgress == 360) {
                        mProgress = 0;
                        isNext = !isNext;
                    }
                    // 刷新View，这里会将View弹栈
                    postInvalidate();
                    try {
                        Thread.sleep(speed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;
        int radius = center - circleWidth / 2;
        mPaint.setStrokeWidth(circleWidth);
        mPaint.setAntiAlias(true);// 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE);// 空心
        // 规定圆的范围，设置画圆的范围
        oval.set(center - radius, center - radius, center + radius, center + radius);
        if (isNext) {
            mPaint.setColor(firstColor);
            // 圆心，半径，画笔
            canvas.drawCircle(center, center, radius, mPaint);
            mPaint.setColor(secondColor);
            canvas.drawArc(oval, -90, mProgress, false, mPaint);
        } else {
            mPaint.setColor(secondColor);
            // 圆心，半径，画笔
            canvas.drawCircle(center, center, radius, mPaint);
            mPaint.setColor(firstColor);
            canvas.drawArc(oval, -90, mProgress, false, mPaint);
        }
    }
}
